<?php

namespace app\admin\controller;

use app\extend\Random;

class User extends Base
{
    protected function _infoModule()
    {
        return array(
            'info' => array(
                'name' => '会员管理',
                'description' => '管理网站后台管理员',
            ),
            'menu' => array(
                array(
                    'name' => '列表',
                    'url' => url('user/index'),
                    'icon' => 'list',
                ),
            ),
            '_info' => array(
                array(
                    'name' => '添加',
                    'url' => url('user/info'),
                ),
            ),
        );
    }

    public function index()
    {
        $request = $this->request;

        //筛选条件
        $where = [];
        $pageMaps = [];

        $id = $request->post('id');
        $username = $request->post('username');
        $status = $request->post('status');

        if ($id) {
            $where['id'] = $id;
        }
        if ($username) {
            $where['username'] = $username;
        }
        if ($status) {
            $where['status'] = $status;
        }

        //URL参数
        $pageMaps['id'] = $id;
        $pageMaps['username'] = $username;
        $pageMaps['status'] = $status;

        $pageAry = [
            'list_rows' => 10,
            'page' => $request->get('page', 1),
            'path' => 'index',
            'query' => $pageMaps,
        ];

        //查询数据
        $list = model('User')->loadList($where, $pageAry);
        $this->assign('list', $list);
        $this->assign('_page', $list->render());
        $this->assign('pageMaps', $pageMaps);

        $this->fetch('index');
    }

    public function info()
    {
        $request = $this->request;
        $method = $request->method();
        $userModel = model('User');
        if ($method == 'POST') {
            $id = $request->post('id');
            if ($id) {
                $status = $userModel->editAll($request);
            } else {
                $status = $userModel->addall($request);
            }
            if ($status !== false) {
                jsonApi(1, '操作成功', ['url'=>url('user/index')]);
            } else {
                jsonApi(0, '操作失败');
            }
        } else {
            $id = $request->get('id');
            $info = $userModel->getInfo($id);
            $this->assign('info', $info);

            $this->fetch('info');
        }
    }

    public function password()
    {
        $request = $this->request;
        $method = $request->method();
        $userModel = model('User');
        if ($method == 'POST') {
            $status = false;
            $tokenTime = date('Ymd') . ceil(date('H')/2);

            $id = $request->post('id');
            $password = $request->post('password');
            $twopassword = $request->post('twopassword');
            $token = $request->post('token');

            if($password != $twopassword){
                jsonApi(0,'两次密码输入不一致');
            }
            if(strlen($password) < 6 || strlen($password) > 16){
                jsonApi(0,'密码长度太短或太长');
            }
            if($token != $tokenTime){
                jsonApi(0,'口令错误');
            }

            if ($id){
                $Random = new Random();
                $salt = strtolower($Random::strnum(22));
                $password = md5(md5($password) . $salt . $id);

                $temp['id'] = $id;
                $temp['password'] = $password;
                $temp['salt'] = $salt;
                $status = $userModel->saved($temp);
            }
            if($status!==false){
                jsonApi(1,'操作成功', ['url'=>url('user/index')]);
            }else{
                jsonApi(0,'操作失败');
            }
        }else{
            $id = $request->get('id');
            $this->assign('id',$id);

            $this->fetch('password');
        }
    }

    /**
     * 删除信息
     */
    public function del()
    {
        $request = $this->request;
        $id = $request->post('id');
        if (empty($id)) {
            jsonApi(0, '参数不能为空');
        }

        if (model('User')->del($id)) {
            jsonApi(1, '删除成功！');
        } else {
            jsonApi(0, '删除失败');
        }
    }

}
